package com.zd.controller;

import com.zd.domain.Collect;
import com.zd.domain.Course;
import com.zd.domain.GoodState;
import com.zd.domain.Result;
import com.zd.service.collect.CollectService;
import com.zd.service.course.CourseService;
import com.zd.service.goodstate.GoodStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private GoodStateService goodStateService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //查询用户全部的收藏课程
    @CrossOrigin
    @GetMapping("/myCollect")
    public Result<List<Collect>> MyCollectList(@RequestParam("userId") int userId) {

        Result<List<Collect>> result = new Result<>();
        String findKey = userId + "collects";
        List<Collect> collects = (List<Collect>) redisTemplate.boundValueOps(findKey).get();
        if (collects == null) {
            List list = collectService.MyCollectList(userId);
            List<Collect> CollectList = new ArrayList<>();
            if (list.size() > 0) {
                for (Object o : list) {
                    int goodId = (int) o;
                    Collect collect = new Collect();
                    Course course = courseService.findCourseById(goodId);
                    collect.setCourse(course);
                    CollectList.add(collect);
                }
                result.setCode(200);
                result.setMsg("查找成功！~");
                result.setData(CollectList);
                redisTemplate.boundValueOps(findKey).set(CollectList, 1, TimeUnit.MINUTES);
            } else {
                result.setCode(411);
                result.setMsg("暂无收藏课程！~");
            }
        } else {
            result.setCode(200);
            result.setMsg("查找成功！~");
            result.setData(collects);
        }
        return result;
    }

    /**
     * 收藏课程
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("/collectCourse")
    public Result collectCourse(@RequestParam("userId") int userId,
                                @RequestParam("goodId") int goodId) {
        Result result = new Result();
        System.out.println(userId);
        System.out.println(goodId);
        //先去查找有没有条信息，有的话直接返回来，没有的话创建一个
        GoodState goodState = goodStateService.findGoodState(userId, goodId);
        //查找回来之后在进行修改
        int influenceRows = collectService.collectCourse(userId, goodId);
        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("收藏失败！~");
        } else {
            result.setCode(200);
            result.setMsg("收藏成功！~");
            //更新redis
            List list = collectService.MyCollectList(userId);
            List<Collect> CollectList = new ArrayList<>();
            for (Object o : list) {
                int goodIds = (int) o;
                Collect collect = new Collect();
                Course course = courseService.findCourseById(goodIds);
                collect.setCourse(course);
                CollectList.add(collect);
            }
            String findKey = userId + "collects";
            redisTemplate.boundValueOps(findKey).set(CollectList,1,TimeUnit.MINUTES);
        }
        return result;
    }

    /**
     * 取消收藏
     * @param userId
     * @param goodId
     * @return
     */
    @CrossOrigin
    @GetMapping("/noCollectCourse")
    public Result noCollectCourse(@RequestParam("userId") int userId,
                                  @RequestParam("goodId") int goodId) {
        Result result = new Result();
        int influenceRows = collectService.noCollectCourse(userId, goodId);
        if (influenceRows == 0) {
            result.setCode(411);
            result.setMsg("取消收藏失败！~");
        } else {
            result.setCode(200);
            result.setMsg("取消收藏成功！~");
            //查询最新数据存入redis中
            List list = collectService.MyCollectList(userId);
            List<Collect> CollectList = new ArrayList<>();
            for (Object o : list) {
                int goodIds = (int) o;
                Collect collect = new Collect();
                Course course = courseService.findCourseById(goodIds);
                collect.setCourse(course);
                CollectList.add(collect);
            }
            String findKey = userId + "collects";
            redisTemplate.boundValueOps(findKey).set(CollectList,1,TimeUnit.MINUTES);
        }
        return result;
    }
}
